package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import kong.unirest.HttpResponse;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;

/**
 * @author Liang Jianlin
 * @date 2024-05-19 16:37
 */
public class ScoreEntryController {
    public VBox content;
    public Button confirm;
    public Button exit;
    public Label courseName;
    public Label outputLabel;
    private String courseType;
    private JSONArray scoreList = new JSONArray();
    private String courseId;
    @FXML
    public void initialize(){
        confirm.setOnAction(e->{
            HttpResponse scoresRes = HttpUtil.put(Globalvar.baseUrl+"/api/courses/"+courseId+"/scores")
                    .header("Content-Type", "application/json")
                    .body(scoreList)
                    .asJson();
            if(scoresRes.getStatus() == 200){
                outputLabel.setText("录入成功");
            }else{
                outputLabel.setText("录入失败");
            }
        });
    }
    public void set(String courseId ) throws IOException {
        this.courseId = courseId;
        JSONObject res = HttpUtil.get(Globalvar.baseUrl+"/api/courses/"+courseId)
                .header("Accept", "application/json")
                .asJson()
                .getBody()
                .getObject();
        System.out.println(res);
        courseType = String.valueOf(res.get("assessment")).equals("GRADE")?"考核":"考试";
        JSONArray scoresRes = HttpUtil.get(Globalvar.baseUrl+"/api/courses/"+courseId+"/scores")
                .header("Accept", "application/json")
                .asJson()
                .getBody()
                .getArray();
        for(int i = 0 ; i < scoresRes.length();i++){
            System.out.println(scoresRes);
            JSONObject JO = scoresRes.getJSONObject(i);
            System.out.println(JO);
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/student-score.fxml"));
            Parent root = fxmlLoader.load();
            StudentScoreController SCS = fxmlLoader.getController();
            SCS.set(JO.getJSONObject("student").getString("name"), (Long) JO.get("studentId"),courseType.equals("考核")?(JO.isNull("grade")?null:JO.getString("grade")):(JO.isNull("score")?null:JO.getString("score")),courseType);
            JSONObject outPutJO = new JSONObject();
            SCS.upload.setOnAction(e->{
                for (int j = 0; j < scoreList.length(); j++) {
                    if(((Long)scoreList.getJSONObject(j).get("studentId"))-0 == ((Long)JO.get("studentId")-0)){
                        scoreList.remove(j);
                        break;
                    }
                }
                outPutJO.put("studentId",(Long) JO.get("studentId"));
                if(courseType.equals("考核")){
                    outPutJO.put("grade",(String) SCS.gradeComboBox.getValue());
                    SCS.score.setText((String) SCS.gradeComboBox.getValue());
                    SCS.gradeComboBox.setVisible(false);
                    SCS.gradeComboBox.setManaged(false);
                }else{
                    outPutJO.put("score",Double.valueOf(SCS.scoreFiled.getText()));
                    SCS.score.setText(SCS.scoreFiled.getText());
                    SCS.scoreFiled.setVisible(false);
                    SCS.scoreFiled.setManaged(false);
                }
                SCS.upload.setVisible(false);
                SCS.upload.setManaged(false);
                SCS.edit.setManaged(true);
                SCS.edit.setVisible(true);
                scoreList.put(outPutJO);
                System.out.println(scoreList);
            });
            content.getChildren().add(root);
        }
    }
}
